<?php

/*
 * 投递器,执行router下的信息.
 * and open the template in the editor.
 */
class Dispatcher
{
    
    static public function dispatch($router)
    {
        global $_G;
        $ctrl=  ucfirst($router->getControll());
      
        if(!self::isexist($ctrl))
        {
            $ctrl='Basecontroll';
            $action='page404';
            Debugshow::show("控制器或方法不存在!");
            
        }
        else
        {
          $action=$router->getAction();
       
        }
        
        $pars=$router->getParams();  
        $obj=new $ctrl();
        if(!method_exists($obj, "setParams")){
            Debugshow::show("无效路径,不可访问!");
            exit(0);
        }else{
            $obj->setParams($pars);
        }
        if(!method_exists($obj,$action))
        {
            Debugshow::show("{$action}方法不存在!");
            $action='page404';
        }
        if(method_exists($obj,$action)){
            $_G["web"]["controll"]= $router->getControll();
            $_G["web"]["action"]= $router->getAction();

            $obj->$action();
        }else{
            Debugshow::show("{$action}方法不存在!");
            exit(0);
        }
       
       
        
    }
    static public function isexist($ctrl){
        $flag=FALSE;
         global $_G;
         $auto_class_path=$_G["auto_load_class_path"];
          foreach ($auto_class_path as $v){
               if(file_exists(ROOT.$v.$ctrl.".php"))
               {
                   $flag=TRUE;
                   break;
               }
          }
          return $flag;
    }
}
?>
